from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet


from goods.models import SKU, GoodsCategory,SPU, SPUSpecification
from meiduo_admin.my_paginate import MyPageNumberPagination
from  meiduo_admin.good import sku_serializers


# 1,sku管理
class SKUViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    serializer_class = sku_serializers.SKUSerializer
    # queryset = SKU.objects.all()

    # 1,重写get_queryset过滤数据
    def get_queryset(self):
        # 1,获取查询关键字
        keyword = self.request.query_params.get("keyword")

        # 2,判断是否有关键字
        if keyword:
            return SKU.objects.filter(name__contains=keyword).all()
        else:
            return SKU.objects.all()


# 2,sku分类数据
class SKUCategoryView(ListAPIView):
    serializer_class = sku_serializers.SKUCategorySerializer
    queryset = GoodsCategory.objects.filter(subs__isnull=True).all()


# 3,sku,spu表数据
class SKUSPUSimpleView(ListAPIView):
    serializer_class = sku_serializers.SKUSPUSimpleSerializer
    queryset = SPU.objects.all()

# 4.spu specification信息
class SPUSpecificationView(ListAPIView):
    serializer_class = sku_serializers.SKUSPUSimpleSerializer
    # queryset = SPUSpecification

    def get_queryset(self):
        # 1,获取spu_id
        spu_id = self.kwargs.get('spu_id')

        # 2.返回规格信息
        return SPUSpecification.objects.filter(spu_id=spu_id).all()